package com.rcloud.stock.adapter;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

import com.rcloud.stock.R;
import com.rcloud.stock.bean.Stock;

public class TableRecyclerAdapter extends ListAdapter<Stock,TableRecyclerAdapter.ViewHolder> {

    public TableRecyclerAdapter() {
        super(new DiffUtil.ItemCallback<Stock>() {
            @Override
            public boolean areItemsTheSame(@NonNull Stock oldItem, @NonNull Stock newItem) {
                return oldItem.getCode().equals(newItem.getCode());
            }

            @Override
            public boolean areContentsTheSame(@NonNull Stock oldItem, @NonNull Stock newItem) {
                return oldItem.getName().equals(newItem.getName());
            }
        });
    }

    @NonNull
    @Override
    public TableRecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_table,parent,false);
        ViewHolder holder = new ViewHolder(view);
        holder.itemView.setOnClickListener(v -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable("stock",(Stock) v.getTag(R.integer.history_tag));
            NavController controller = Navigation.findNavController(v);
            controller.navigate(R.id.action_hqFragment_to_detailFragment,bundle);
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull TableRecyclerAdapter.ViewHolder holder, int position) {
        Stock stock = getItem(position);
        holder.itemView.setTag(R.integer.history_tag,stock);
        holder.textViews[0].setText(stock.getCode());
        holder.textViews[1].setText(stock.getName());
        holder.textViews[2].setText(String.valueOf(stock.getLb()));
        holder.textViews[3].setText(String.valueOf(stock.getZde()));
        holder.textViews[4].setText(String.valueOf(stock.getZdf()));
        holder.textViews[5].setText(String.valueOf(stock.getCjl()));
        holder.textViews[6].setText(String.valueOf(stock.getCje()));
        holder.textViews[7].setText(String.valueOf(stock.getZf()));
        holder.textViews[8].setText(String.valueOf(stock.getHigh()));
        holder.textViews[9].setText(String.valueOf(stock.getLow()));
        holder.textViews[10].setText(String.valueOf(stock.getHsl()));
        holder.textViews[11].setText(String.valueOf(stock.getSyl()));
        holder.textViews[12].setText(String.valueOf(stock.getTmv()));
        holder.textViews[13].setText(String.valueOf(stock.getSjl()));
        holder.textViews[14].setText(String.valueOf(stock.getTo()));
        holder.textViews[15].setText(String.valueOf(stock.getZs()));
        holder.textViews[16].setText(String.valueOf(stock.getLb()));
        holder.textViews[17].setText(stock.getTime());
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView[] textViews = new TextView[18];
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            textViews[0] = itemView.findViewById(R.id.textView2);
            textViews[1] = itemView.findViewById(R.id.textView3);
            textViews[2] = itemView.findViewById(R.id.textView4);
            textViews[3] = itemView.findViewById(R.id.textView5);
            textViews[4] = itemView.findViewById(R.id.textView6);
            textViews[5] = itemView.findViewById(R.id.textView7);
            textViews[6] = itemView.findViewById(R.id.textView8);
            textViews[7] = itemView.findViewById(R.id.textView9);
            textViews[8] = itemView.findViewById(R.id.textView10);
            textViews[9] = itemView.findViewById(R.id.textView11);
            textViews[10] = itemView.findViewById(R.id.textView12);
            textViews[11] = itemView.findViewById(R.id.textView13);
            textViews[12] = itemView.findViewById(R.id.textView14);
            textViews[13] = itemView.findViewById(R.id.textView15);
            textViews[14] = itemView.findViewById(R.id.textView16);
            textViews[15] = itemView.findViewById(R.id.textView17);
            textViews[16] = itemView.findViewById(R.id.textView18);
            textViews[17] = itemView.findViewById(R.id.textView19);
        }
    }
}
